﻿using System;
using System.Collections.Generic;
using SFML.Graphics;
using Color = Transformable_Engine_v2.Engine.Graphics.Color;

namespace Transformable_Engine_v2.Engine.Rendering
{
    public static class ShaderHandler
    {
        private static Dictionary<string, SFML.Graphics.Shader> _shaderDictionary;
        private static string _currentShaderName;

        public static bool HasShader { get; set; }

        public static void Initialize()
        {
            HasShader = false;
            _currentShaderName = null;

            _shaderDictionary = new Dictionary<string, SFML.Graphics.Shader>();
        }

        public static void AddShader(string shaderName, string vertexShaderPath, string fragmentShaderPath)
        {
            SFML.Graphics.Shader s = new SFML.Graphics.Shader(vertexShaderPath, fragmentShaderPath);

            _shaderDictionary.Add(shaderName, s);
        }

        public static void SetParameter(string shaderName, string parameterName, Color colorParameter)
        {
            _shaderDictionary[shaderName].SetParameter(parameterName, colorParameter.ToSFMLColor());
        }
        public static void SetParameter(string shaderName, string parameterName, float floatParameter)
        {
            _shaderDictionary[shaderName].SetParameter(parameterName, floatParameter);
        }

        public static void SetShader(string shaderName)
        {
            _currentShaderName = shaderName;
            HasShader = true;
        }

        public static SFML.Graphics.RenderStates CurrentShader
        {
            get
            {
                if(!HasShader)
                    throw new Exception("The shader must be set before it is accessed");
                return new RenderStates(_shaderDictionary[_currentShaderName]);
            }
        }

        public static void RemoveShader()
        {
            HasShader = false;
        }
    }
}